package cn.iocoder.yudao.module.system.dal.mysql.a_quest;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.a_quest.vo.QuestExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.a_quest.vo.QuestListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.a_quest.vo.QuestPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.a_quest.QuestDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.Arrays;
import java.util.List;

/**
 * 待办 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface QuestMapper extends BaseMapperX<QuestDO> {

    default PageResult<QuestDO> selectPageSuccess(QuestPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<QuestDO>()
                        .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
                        .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                        .eqIfPresent(QuestDO::getStatus, 2)
                        .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                        //.betweenIfPresent(QuestDO::getCreatetime, reqVO.getCreatetime())
                        .orderByDesc(QuestDO::getCreatetime)
                //.orderByDesc(QuestDO::getId)
        );
    }

    default PageResult<QuestDO> selectPage(QuestPageReqVO reqVO) {
        if (reqVO.getStatus() != null) {
            return selectPage(reqVO, new LambdaQueryWrapperX<QuestDO>()
                            .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
                            .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                            .eqIfPresent(QuestDO::getStatus, reqVO.getStatus())
                            .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                            //.betweenIfPresent(QuestDO::getCreatetime, reqVO.getCreatetime())
                            .orderByDesc(QuestDO::getCreatetime)
                    //.orderByDesc(QuestDO::getId)
            );
        }
        return selectPage(reqVO, new LambdaQueryWrapperX<QuestDO>()
                        .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
                        .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                        .eqIfPresent(QuestDO::getStatus, reqVO.getStatus())
                        .in(QuestDO::getStatus, Arrays.asList(0, 1))
                        .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                        //.betweenIfPresent(QuestDO::getCreatetime, reqVO.getCreatetime())
                        .orderByDesc(QuestDO::getCreatetime)
                //.orderByDesc(QuestDO::getId)
        );
    }

    default List<QuestDO> selectList(QuestExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<QuestDO>()
                        .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
                        .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                        .eqIfPresent(QuestDO::getStatus, reqVO.getStatus())
                        .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                        .betweenIfPresent(QuestDO::getCreatetime, reqVO.getCreatetime())
                //.orderByDesc(QuestDO::getId)
        );
    }

//    default List<QuestDO> selectQueryList(QuestListReqVO reqVO) {
//        return selectPage(reqVO,new LambdaQueryWrapperX<QuestDO>()
//                .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
//                .eq(QuestDO::getPid,"0"));
//    }

    default PageResult<QuestDO> selectQueryList(QuestListReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<QuestDO>()
                        .likeIfPresent(QuestDO::getQuestname, reqVO.getQuestname())
                        .eqIfPresent(QuestDO::getPid, "0")
                        .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                        .orderByDesc(QuestDO::getCreatetime)
        );
    }

    default List<QuestDO> selectQuestChildrenList(QuestListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<QuestDO>()
                .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                .eqIfPresent(QuestDO::getStatus, reqVO.getStatus())
                .ne(QuestDO::getPid,"0")
                .eq(QuestDO::getPid,reqVO.getPid())
                .eq(QuestDO::getTransactor, WebFrameworkUtils.getLoginUserId())
                .orderByDesc(QuestDO::getCreatetime));
    }

    default List<QuestDO> selectErpQuestChildrenList(QuestListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<QuestDO>()
                .eqIfPresent(QuestDO::getQuesttype, reqVO.getQuesttype())
                .eqIfPresent(QuestDO::getStatus, reqVO.getStatus())
                .ne(QuestDO::getPid,"0")
                .eq(QuestDO::getPid,reqVO.getPid())
                .orderByDesc(QuestDO::getCreatetime));
    }

    //即将逾期
    @Select("select * from quest where DATEDIFF(endtime, NOW())<=3 AND transactor=#{transactor} AND status!=2  order by endtime desc")
    List<QuestDO> getEndTimeQuest(@Param("transactor") String transactor);

    //本月代办
    @Select("SELECT * FROM quest WHERE MONTH(createtime) = MONTH(NOW()) AND (status=0 or status=1) AND transactor=#{transactor}  order by createtime desc")
    List<QuestDO> getMonthQuest(@Param("transactor") String transactor);

    //待办总数
    @Select("SELECT COUNT(1) FROM quest WHERE (status=0 or status=1) AND transactor=#{transactor}")
    Integer countQuest(@Param("transactor") String transactor);

    //即将逾期总数
    @Select("SELECT COUNT(1) FROM quest WHERE DATEDIFF(endtime, NOW())<=3 AND transactor=#{transactor} AND status!=2")
    Integer countEndTimeQuest(@Param("transactor") String transactor);

    //本月代办
    @Select("SELECT COUNT(1) FROM quest WHERE  pid=#{pid}")
    Integer isExitChildren(@Param("pid") Integer pid);

    default PageResult<QuestDO> selectErpQueryList(QuestListReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<QuestDO>()
                .eq(QuestDO::getQuestname, reqVO.getQuestname())
                .eq(QuestDO::getErpid, reqVO.getErpid())
                .eqIfPresent(QuestDO::getPid, "0")
                .orderByDesc(QuestDO::getCreatetime)
        );
    }

}
